// 子弹实体类
class Bullet {
    constructor(id, tankId, playerId, x, y, width, height, velocityX, velocityY, color) {
        this.id = id;
        this.tankId = tankId;  // 发射子弹的坦克ID
        this.playerId = playerId;  // 发射子弹的玩家ID
        this.x = x;
        this.y = y;
        this.width = width || 10;
        this.height = height || 10;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
        this.color = color || '#FF0000';
        this.damage = 25;  // 子弹伤害值
        this.active = true;  // 子弹是否有效
    }

    // 更新子弹位置
    update(deltaTime) {
        if (!this.active) return;
        
        // 根据速度更新位置
        this.x += this.velocityX;
        this.y += this.velocityY;
    }

    // 渲染子弹
    render(ctx) {
        if (!this.active) return;
        
        // 获取渲染系统的偏移量
        const offsetX = ctx.canvas.offsetX || 0;
        const offsetY = ctx.canvas.offsetY || 0;
        
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x + offsetX + this.width / 2, this.y + offsetY + this.height / 2, this.width / 2, 0, Math.PI * 2);
        ctx.fill();
    }

    // 检查子弹是否超出边界
    isOutOfBounds(mapWidth, mapHeight) {
        return (
            this.x < 0 ||
            this.x > mapWidth ||
            this.y < 0 ||
            this.y > mapHeight
        );
    }

    // 碰撞检测
    checkCollision(obj) {
        return (
            this.x < obj.x + obj.width &&
            this.x + this.width > obj.x &&
            this.y < obj.y + obj.height &&
            this.y + this.height > obj.y
        );
    }

    // 销毁子弹
    destroy() {
        this.active = false;
    }
}

// 如果在浏览器环境中，将Bullet添加到window对象
if (typeof window !== 'undefined') {
    window.Bullet = Bullet;
}

// 如果在Node.js环境中，导出Bullet类
if (typeof module !== 'undefined' && module.exports) {
    module.exports = Bullet;
}